<?php

header("Content-type: image/PNG");
imgcode(5,120,25);

function imgcode($nums,$width,$high) {
    session_start();
    //去除了數字0和1 字母小寫O和L，為了避免辨識不清楚
    $str = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMOPQRSTUBWXYZ";
    $code = '';
    for ($i = 0; $i < $nums; $i++) {
        $code .= $str[mt_rand(0, strlen($str)-1)];
    }

    $_SESSION['validate'] = $code;

    //建立圖示，設置寬度及高度與顏色等等條件
    $image = imagecreate($width, $high);
    $black = imagecolorallocate($image, mt_rand(0, 200), mt_rand(0, 200), mt_rand(0, 200));
    $border_color = imagecolorallocate($image, 21, 106, 235);
    $background_color = imagecolorallocate($image, 235, 236, 237);

    //建立圖示背景
    imagefilledrectangle($image, 0, 0, $width, $high, $background_color);

    //建立圖示邊框
   // imagerectangle($image, 0, 0, $width-1, $high-1, $border_color);

    //在圖示布上隨機產生大量躁點
    for ($i = 0; $i < 200; $i++) {
        imagesetpixel($image, rand(0, $width), rand(0, $high), $black);
    }

    $strx = rand(3, 8);
    for ($i = 0; $i < $nums; $i++) {
        $strpos = rand(1, 6);
        imagestring($image, 20, $strx, $strpos, substr($code, $i, 1), $black);

        $strx += rand(10, 30);
    }

    imagepng($image);
    imagedestroy($image);
}

/*
session_start();

header ("Content-type: image/png");
$im = @imagecreatetruecolor(60, 20) or die("Cannot Initialize new GD image stream");
$text_color = imagecolorallocate($im, 255, 214, 191);
$a=array('','A','B','C','D','E','F','G','H','I','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z');
$b=array('','1','2','3','4','5','6','7','8','9');

$text=$a[rand(1,count($a)-1)].$b[rand(1,count($b)-1)].$a[rand(1,count($a)-1)].$b[rand(1,count($b)-1)];
$_SESSION['validate']=$text;
imagestring($im, 5, 10, 3,$text, $text_color);
imagepng($im);
imagedestroy($im);
*/

?>
